$(document).ready(function(){
	// inserting helper tags
	$('body').append('<div class="desa" style="display:none"><textarea NAME="comments" COLS=40 ROWS=6 id="newtext"></textarea><input type="hidden" NAME="id" id="newid"></input><input type=submit VALUE="submit" id="change"><input type="hidden" NAME="id" id="tmpid"></input></div>');
	//inserting the show button 
	$('body').append('<input type=submit class="toggle" id="toggleboy" style="display:none" VALUE="editar" id="editar"><input type=submit class="toggle_new" style="display:none" VALUE="nuevo" id="editar">')
	//insert on every djanamic an edit button
	$('.djynamic').each(function(){
		var id = $(this).attr('id');
		if (id.length>=10 )
		{
			spli = id.split('-');
			var inic = spli[0];
			if (inic == "djynamicup")
			{
				var i = 1;
				var tail = "";
				for (i=1;i<spli.length;i++)
				{	
					
					if( i==spli.length-1)
						tail = tail+spli[i];
					else tail = tail +spli[i] + "-";
				}
// 				var tail = id.substr(9,id.length);
				var new_id = inic + "-link-"+tail;
				$(this).after($('#toggleboy').clone(true).attr({id:new_id}));
				$('#'+new_id).show();
			}
			else if(inic == "djynamicnew")
			{
				$(this).append('<div class="desa_new" style="display:none"><textarea NAME="comments" COLS=40 ROWS=6 id="newtext_new"></textarea><input type="hidden" NAME="id" id="newid_new"></input><input type=submit VALUE="submit" id="change_new"><input type="hidden" NAME="id" id="tmpid_new"></input></div>');
				var i = 1;
				var tail = "";
				for (i=1;i<spli.length;i++)
				{	
					
					if( i==spli.length-1)
						tail = tail+spli[i];
					else tail = tail +spli[i] + "-";
				}
// 				var tail = id.substr(9,id.length);
				var new_id = inic + "-newlink-"+tail;
				$(this).after($('.toggle_new').clone(true).attr({id:new_id}));
				$('#'+new_id).show();
					
			}
		}		

	});

//UPDATE	
	$('.toggle').click(function(){	
		$('.desa').insertBefore(this);
		$('.desa').toggle('slow');
		var id = $(this).attr('id')
		var spli = id.split('-');
		var inic = spli[0];
		var tail = ""
		for (i=2;i<spli.length;i++)
		{	
					
			if( i==spli.length-1)
				tail = tail+spli[i];
			else tail = tail +spli[i] + "-";
		}
		var new_id = inic+'-'+tail;
		$('#newtext').val($('#'+new_id).text());
		$('#tmpid').val(new_id);
		$('#newid').val(tmp);
		
	});

	$('#change').click(function(){	
		var tmp = $('#newtext').val();
		var id = $('#tmpid').val();
		var spli = id.split("-");
		var proy = spli[1];
		var class = spli[2];
		var obj_id = spli[3];
		var attr = spli[4];
		
		$.getJSON("/"+proy+"/djynamyc/",{cls: class,objid: obj_id,att:attr,info:tmp,action:"update"}, 
			function(json){
				var id = $('#tmpid').val();
				$("#"+id).text(json['result']);
				$("#"+id).change();
			}
		);
// 		$('#'+id).text(tmp);
	});
//NEW
	$('.toggle_new').click(function(){	
		$('.desa_new').toggle('slow');
		var id = $(this).attr('id')
		var spli = id.split('-');
		var inic = spli[0];
		var tail = ""
		for (i=2;i<spli.length;i++)
		{	
					
			if( i==spli.length-1)
				tail = tail+spli[i];
			else tail = tail +spli[i] + "-";
		}
		var new_id = inic+'-'+tail;
 		$('#newtext_new').val($('#'+new_id).text());
		$('#tmpid_new').val(new_id);
		$('#newid_new').val(tmp);
		
	});

	$('#change_new').click(function(){	
		var tmp = $('#newtext_new').val();
		var id = $('#tmpid_new').val();
		var spli = id.split("-");
		var proy = spli[1];
		var class = spli[2];
		var attr = spli[3];
		$.getJSON("/"+proy+"/djynamyc/",{cls: class,att:attr,info:tmp, action:"new"}, 
			function(json){
				var id = $('#tmpid_new').val();
// 				alert(id);
// 				$("#"+id).text(json['result']);
				var spli = id.split("-");
				var proy = spli[1];
				var class = spli[2];
				var attr = spli[3];
				var ob_id = json['objidnew'];
				new_id = "djynamycup-"+proy+"-"+class+"-"+ob_id+"-"+attr
				alert(new_id);
				$("#"+id).change();
			}
		);
// 		$('#'+id).text(tmp);
	});
});





// 	$('#djynamic').hover(
//  	function(){
// 		$(this).append($('.toggle').clone(true).attr({id:'djynamic-clase-atributo-id'}));
// 		$('#djynamic-clase-atributo-id').show()
// 		var id = $('#djynamic-clase-atributo-id').attr('id');
// 		if (id.length>=8 )
// 		{
// 			var inic = id.substr(0,8);
// 			if (inic = "djynamic")
// 			{
// 				var tail = id.substr(9,id.length);
// 				new_id = inic + "-link-"+tail;
// 			}
// 		}
//  	},function(){
//   	$('#djynamic-clase-atributo-id').remove();
// 	$('.desa').hide('slow');
// 	}
//  	);